﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using SmartSiteManagementSystem.AdminWeb.Common;
using SmartSiteManagementSystem.Models.Admin;
using SmartSiteManagementSystem.Models.DTO;
using SmartSiteManagementSystem.Utils;
using SmartSiteManagementSystem.IServices;
using SmartSiteManagementSystem.AdminWeb.Filter;

namespace SmartSiteManagementSystem.AdminWeb.Controllers
{
    
    /*TypeFilter和ServiceFilter的区别是TypeFilter不从DI容器中创建实例，所以不需要在ConfigureServices中进行注册就能使用。
并且TypeFilter的生命周期是瞬时的我们无法控制，而ServiceFilter我们可以在注册时自己调整。在控制器上面添加特性ServiceFilter，此特性Order越小，表示越优先执,如果不想所有action都执行，可以添加到相应的action上即可*/
    //[ServiceFilter(typeof(LoginAuthorizeAttribute), Order = 1)]
    //[TypeFilter(typeof(AdminRightAttribute))]
    public class AdminBaseControllers : Controller
    {
        private readonly IAdminService _adminService;
        public AdminBaseControllers(IAdminService adminService)
        {
            _adminService = adminService;
        }

        /// <summary>
        /// 获取CSS版本号
        /// </summary>
        protected string CssVersion
        {
            get
            {
                return SSMSConfig.CssVersion;
            }
        }
        /// <summary>
        /// 获取JS版本号
        /// </summary>
        protected string JsVersion
        {
            get
            {
                return SSMSConfig.JsVersion;
            }
        }

        private AdminUserInfo currentAdminUser;
        /// <summary>
        /// 获取当前登录用户
        /// </summary>
        protected AdminUserInfo CurrentAdminUser
        {
            get
            {
                if (currentAdminUser == null)
                {
                    currentAdminUser = GetCurrentUserInfo();
                }
                return currentAdminUser;
            }
        }

        public AdminUserInfo GetCurrentUserInfo()
        {
            string encryCookieVal = GetCookies(SessionCookieKeyHelper.CookiesUserIdKey);
            string cookieVal = "";
            if(!string.IsNullOrEmpty(encryCookieVal))
            {
                   cookieVal = new DESSecurity().DesDecrypt(encryCookieVal);
            }
            if (!string.IsNullOrEmpty(cookieVal))
            {
                int AdminUserId = Convert.ToInt32(cookieVal);
                return _adminService.GetAdminUserInfoByUserId(AdminUserId);
            }
            return null;
        }


        /// <summary>
        /// 获取cookies
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>返回对应的值</returns>
        private string GetCookies(string key)
        {
            HttpContext.Request.Cookies.TryGetValue(key, out string value);
            if (string.IsNullOrEmpty(value))
            {
                value = string.Empty;
            }
            return value;
        }

        /// <summary>
        /// 第3步 执行父类的异步方法  异步方法  似乎可以和OnActionExecuting同时执行
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            return base.OnActionExecutionAsync(context, next);
        }

        /// <summary>
        /// 第4步 执行OnActionExecuting方法
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            ViewBag.CssVersion = CssVersion;
            ViewBag.JsVersion = JsVersion;
            base.OnActionExecuting(context);
        }

        /// <summary>
        /// 第6步 Aciton执行完毕后 执行 OnActionExecuted
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            base.OnActionExecuted(context);
        }



        /// <summary>
        /// 返回成功JSon结果集
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="IsSuccess"></param>
        /// <param name="Code"></param>
        /// <param name="Msg"></param>
        /// <param name="Data"></param>
        /// <returns></returns>
        protected ResultInfo<T> ResultData<T>(bool IsSuccess, int Code, string Msg, T Data)
        {
            ResultInfo<T> result = new ResultInfo<T>();
            result.IsSuccess = IsSuccess;
            result.Code = Code;
            result.Msg = Msg;
            result.Data = Data;
            return result;
        }

        /// <summary>
        /// 返回失败的JSon结果集
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        protected ResultInfo ResultNoData(bool IsSuccess, int Code, string Msg)
        {
            ResultInfo result = new ResultInfo();
            result.IsSuccess = IsSuccess;
            result.Code = Code;
            result.Msg = Msg;
            return result;
        }


    }
}